/******************************************************************
系统名称: 
类  名  称: FooterMenu.java
软件版权: 恒生电子股份有限公司
系统版本: 1.0
开发人员: maqf
开发时间: 2015-05-04
功能说明: 底部导航
审核人员: 
相关文档: 
修改记录: 需求编号         修改日期         修改人员                     修改说明
 
******************************************************************/
package com.hundsun.fcp.widget;

import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

import com.hundsun.fcp.R;
import com.hundsun.fcp.fragments.AssetsFragments;
import com.hundsun.fcp.fragments.IndexFragments;
import com.hundsun.fcp.fragments.MineFragments;
import com.hundsun.fcp.impl.OnFooterMenuItemListener;
import com.hundsun.fcp.webplugin.FundTradingFunction;
import com.hundsun.fcp.webplugin.FundTradingHelper;


public class FooterMenu implements OnCheckedChangeListener{
	private Fragment  mCurrentFragment;
	private IndexFragments mIndexFragments ;
	private AssetsFragments mAssetsFragments ;
	private MineFragments mMineFragments ;
	/** 显示首页fragment */
	public static final int FRAGMENT_INDEX= 1;
	/** 显示资产fragment */
	public static final int FRAGMENT_ASSETS = 2;
	/** 显示我的fragment */
	public static final int FRAGMENT_MINE = 3;
	private RadioButton firstBt;  
    private RadioButton secondBt;  
    private RadioButton thirdBt;  
	private ActionBarActivity mActivity;
	private int mState = 0;
	
	private View mFooterView ;
	
	private OnFooterMenuItemListener mOnFooterMenuItemListener ;
	
	public FooterMenu(ActionBarActivity activity) {
		mActivity = activity;
		initView();
	}
	
	private void initView() {
		mFooterView = findViewById(R.id.footer) ;
		firstBt = (RadioButton) findViewById(R.id.main_tab_first);
		secondBt = (RadioButton) findViewById(R.id.main_tab_second);
		thirdBt = (RadioButton) findViewById(R.id.main_tab_three);
		
		firstBt.setOnCheckedChangeListener(this) ;
		secondBt.setOnCheckedChangeListener(this);
		thirdBt.setOnCheckedChangeListener(this) ;
	}

	private View findViewById(int id) {
		return mActivity.findViewById(id);
	}
	
	/**
	 * 打开首页
	 */
	public void switchHome(){
		switchState(FRAGMENT_INDEX) ;
	}
	
	/**
	 * 打开资产
	 */
	public void switchAssets(){
		switchState(FRAGMENT_ASSETS) ;
	}
	
	/**
	 * 打开我的
	 */
	public void switchMine(){
		switchState(FRAGMENT_MINE) ;
	}
 
    private void switchState(int state) {
        if (mState == state) {
            return;
        } // else continue
 
        mState = state;
        firstBt.setChecked(false);
        secondBt.setChecked(false);
        thirdBt.setChecked(false);
 
        switch (mState) {
            case FRAGMENT_INDEX:
                firstBt.setChecked(true);
                break;
 
            case FRAGMENT_ASSETS:
                secondBt.setChecked(true);
                break;
 
            case FRAGMENT_MINE:
                thirdBt.setChecked(true);
                break;
  
            default:
                break;
        }
        
        switchContent(mState) ;
        
        if(mOnFooterMenuItemListener!=null){
        	mOnFooterMenuItemListener.onItemClick(mState) ;
        }
        
        
    }
 
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
            case R.id.main_tab_first:
                switchState(FRAGMENT_INDEX);
                break;
            case R.id.main_tab_second:
                switchState(FRAGMENT_ASSETS);
                break;
            case R.id.main_tab_three:
                switchState(FRAGMENT_MINE);
                break;
            default:
                break;
            }
        }
    }

	public OnFooterMenuItemListener getOnFooterMenuItemListener() {
		return mOnFooterMenuItemListener;
	}

	public void setOnFooterMenuItemListener(OnFooterMenuItemListener mOnFooterMenuItemListener) {
		this.mOnFooterMenuItemListener = mOnFooterMenuItemListener;
	}
	
	/**
	 * 切换内容
	 * @param fragmentTag
	 */
	private void switchContent(int fragmentTag) {
	
		if (mCurrentFragment != null) {
			mActivity.getSupportFragmentManager().beginTransaction().detach(mCurrentFragment).commit();
		}
		Fragment f= mActivity.getSupportFragmentManager().findFragmentByTag(String.valueOf(fragmentTag));
		
		if (f == null) {
			f = createFragment(fragmentTag);
		} 
		if (!f.isAdded()) {
			mActivity.getSupportFragmentManager().beginTransaction().add(R.id.main_content, f, String.valueOf(fragmentTag)).attach(f).commitAllowingStateLoss();
		}else{
			mActivity.getSupportFragmentManager().beginTransaction().attach(f).commitAllowingStateLoss();
		}
		mCurrentFragment = f;
		
	}
	
	/**
	 * 创建Fragment
	 * @param fragmentTag
	 * @return
	 */
	private Fragment createFragment(int fragmentTag) {
		Fragment f = null;
		if (FooterMenu.FRAGMENT_INDEX == fragmentTag) {
			if(mIndexFragments==null)
				mIndexFragments = new IndexFragments();
			f = mIndexFragments ;
		} else if (FooterMenu.FRAGMENT_ASSETS== fragmentTag) {
			if(mAssetsFragments==null)
				mAssetsFragments= new AssetsFragments();
			mAssetsFragments.setArguments(FundTradingHelper.getFundTradingArguments(FundTradingFunction.ASSETS, null)) ;
			f = mAssetsFragments ;
		} else if (FooterMenu.FRAGMENT_MINE==fragmentTag) {
			if(mMineFragments == null)
				mMineFragments = new MineFragments() ;
			f = mMineFragments;
		} 
	 
		return f;
	}
	
	public void show(boolean show){
		mFooterView.setVisibility(show?View.VISIBLE:View.GONE) ;
	}
	
	public boolean back(){
		if(mCurrentFragment instanceof AssetsFragments){
			AssetsFragments fragments = (AssetsFragments) mCurrentFragment ;
			if(fragments.canBack()){
				fragments.back() ;
				return true ;
			}
		}
		return false ;
	}
}
